/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.cloudfarming.client.util;


import java.io.FileNotFoundException;
import java.security.NoSuchAlgorithmException;
import java.io.File;
import java.io.IOException;
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

/**
 *
 * @author Merijn Zengers & Gerben Feenstra
 */
public class FileCopierIT {
    private static final String SOURCEDIRECTORY_STRING = "fromdir";
    private static final String TESTSUBDIRECTORY_STRING = SOURCEDIRECTORY_STRING + File.separator + "testdir";
    private static final String DESTINATIONDIRECTORY_STRING = "todir";

    private final File sourceDirectory = new File(SOURCEDIRECTORY_STRING);;
    private final File testDirectory = new File(TESTSUBDIRECTORY_STRING);
    private final File destinationDirectory = new File(DESTINATIONDIRECTORY_STRING) ;
    
    private static final String FILENAME1 = "file1.ext";
    private static final String FILENAME2 = "file2.ext";    
    private static final String SHP_FILENAME = "file.shp";
    private static final String DBF_FILENAME = "file.dbf";

    private File file1 = new File(SOURCEDIRECTORY_STRING + File.separator + FILENAME1);
    private File file2 = new File(TESTSUBDIRECTORY_STRING + File.separator + FILENAME2);
    private File fileShp = new File(SOURCEDIRECTORY_STRING + File.separator + SHP_FILENAME);
    private File fileDbf = new File(SOURCEDIRECTORY_STRING + File.separator + DBF_FILENAME);


    @Before
    public void setUp() throws IOException{      
        // Make Directories
        sourceDirectory.mkdir();
        destinationDirectory.mkdir();
        testDirectory.mkdir();

        // Create files
        file1.createNewFile();     
        file2.createNewFile();
        fileShp.createNewFile();
        fileDbf.createNewFile();        
    }
     
    @After
    public void tearDown(){ 
        FileUtil.removeTree(destinationDirectory);
        FileUtil.removeTree(sourceDirectory);
    }
    
    // Test simple file copy
    @Test
    public void copyFile() throws NotADirectoryException, NotAFileException, IOException, FileNotFoundException, DirectoryCreationException, NoSuchAlgorithmException{
        FileCopier.copyFile(file1, destinationDirectory);
        File destFile = new File(destinationDirectory.getPath() + File.separator + file1.getName());
        
        // File must exist in both locations
        assertTrue(file1.exists());
        assertTrue(destFile.exists());
    }

    // Now with a bundled file
    @Test
    public void copyBundledFile() throws NotADirectoryException, NotAFileException, IOException, FileNotFoundException, DirectoryCreationException, NoSuchAlgorithmException{
        FileCopier.copyFile(fileShp, destinationDirectory, true); // True indicates Bundled, ie, copy all files with same name but different ext.
        
        File destShp = new File(destinationDirectory.getPath() + File.separator + fileShp.getName());
        File destDbf = new File(destinationDirectory.getPath() + File.separator + fileDbf.getName());

        // File must exist in both locations
        assertTrue(fileShp.exists());
        assertTrue(destShp.exists());
        // DBF should be copied too
        assertTrue(destDbf.exists());
    }
        
        
    // Test copyFile with directory as input, should FAIL
    @Test (expected = NotAFileException.class)
    public void copyFileWithDirectoryAsInput() throws NotADirectoryException, NotAFileException, IOException, FileNotFoundException, DirectoryCreationException, NoSuchAlgorithmException {
        // Should throw an exception
        FileCopier.copyFile(testDirectory, destinationDirectory);
    }

    // Test copyFile (bundled file) with directory as input
    @Test (expected = NotAFileException.class)
    public void copyShapeFileWithDirectoryAsInput() throws NotADirectoryException, NotAFileException, IOException, FileNotFoundException, DirectoryCreationException, NoSuchAlgorithmException {
        // Should throw an exception
        FileCopier.copyFile(testDirectory, destinationDirectory, true);
    }
    

    // Copy a whole directory including content
    @Test
    public void copyDirectory() throws NotADirectoryException, NotAFileException, IOException, FileNotFoundException, DirectoryCreationException, NoSuchAlgorithmException {     
        FileCopier.copyDirectory(testDirectory, destinationDirectory, null, false);      
        File destination = new File(destinationDirectory.getPath() + File.separator + testDirectory.getName());
        
        assertTrue(destination.exists());
        //File in testDirectory should be copied to destination too
        assertEquals(testDirectory.listFiles()[0].getName(), destination.listFiles()[0].getName());
    }

    @Test (expected = NotADirectoryException.class)
    public void copyDirectoryWithFileAsInput() throws NotADirectoryException, NotAFileException, IOException, FileNotFoundException, DirectoryCreationException, NoSuchAlgorithmException{
        FileCopier.copyDirectory(file1, destinationDirectory, null, false);
    }
    
    @Test (expected = NotADirectoryException.class)
    public void copyShapeDirectoryWithFileAsInput() throws NotADirectoryException, NotAFileException, IOException, FileNotFoundException, DirectoryCreationException, NoSuchAlgorithmException {
        FileCopier.copyDirectory(file1, destinationDirectory, ".shp", true);
    }
   
    
    /*
     * 
     * 
     * 
     * MOVE METHODS ARE NOT USED AS OF YET
     * 
    @Test
    public void moveFileTestGeneric() throws NotADirectoryException, NotAFileException, DirectoryCreationException, FileMoveException, IOException{

        FileCopier.checkIsFile(file1);
        
       
        // Copy file to test destination directory
        FileCopier.moveFile(file1, destinationDirectory, false, false);

        // Create File-object of destination-file, check if it exists
        File destFile = new File(destinationDirectory.getPath() + File.separator + file1.getName());
        assertTrue(destFile.exists());
    }

    @Test (expected = NotAFileException.class)
    public void moveFileWithNotAFileExceptionGeneric() throws NotADirectoryException, NotAFileException, FileMoveException, DirectoryCreationException{
        //SetUP toDirectory
        FileCopier.moveFile(sourceDirectory, destinationDirectory, false, false);
    }
     */
    
    
/*
    @Test
    public void moveFileGeneric() throws NotADirectoryException, NotAFileException, DirectoryCreationException, FileMoveException, IOException{

         FileCopier.moveFile(file1,destinationDirectory, true, false);
         File destFile = new File(destinationDirectory.getPath() + File.separator + file1.getName());

         // File must exist only in destination
         assertTrue(!file1.exists());
         assertTrue(destFile.exists());
    }
     
     */

    /*
//    @Test (expected = FileMoveException.class)
// TODO: Theck why this test does not run on hudson
    public void moveFileWithOverwriteFalseGeneric() throws NotADirectoryException, NotAFileException, IOException, FileNotFoundException, DirectoryCreationException, NoSuchAlgorithmException, FileMoveException{
         // First, copy file to destination
        FileCopier.copyFile(file1, destinationDirectory, null, false);

        // Try overwrite, should fail
        FileCopier.moveFile(file1,destinationDirectory, false, false);
    }
     * 
     */

    /*
    @Test
    public void moveFileWithOverwriteTrueGeneric() throws NotADirectoryException, NotAFileException, IOException, FileNotFoundException, DirectoryCreationException, NoSuchAlgorithmException, FileMoveException{
         // First, copy file to destination
        FileCopier.copyFile(file1, destinationDirectory, null, false);

        // Try overwrite, should not fail
        FileCopier.moveFile(file1,destinationDirectory, true, false);

        File destFile = new File(destinationDirectory.getPath() + File.separator + file1.getName());

        // File must exist only in destination (overwritten the existing file)
        assertTrue(!file1.exists());
        assertTrue(destFile.exists());

    }
*/
}
